#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "zio.h"

int main(int argc, char *argv[])
{
    int rc = 0;

    while (argc > 1) {
	FILE *file;
	char line[1024];
	size_t len;

	argv++;
	argc--;

	if (!(file = fzopen(*argv, "w6"))) {
	    fprintf(stderr, "fzopen: %s: %s\n", *argv, strerror(errno));
	    continue;
	}

	while ((len = fread(line, sizeof(char), sizeof (line), stdin))) {
	    size_t ret = fwrite(line, sizeof(char), len, file);
	    if ((ret != len)) {
		rc = 1;
		fprintf(stderr, "fwrite: %s: %zu < %zu: %s\n",
		    *argv, ret, len, strerror(errno));
		break;
	    }
	}

	if (fclose(file)) {
	    rc = 1;
	    fprintf(stderr, "fclose: %s: %s\n", *argv, strerror(errno));
	}
    }

    return rc;
}
